var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 团订单数据
    teamOrder: [],
    // 加载图标的大小
    sysHeight: 0,
    // 控制加载
    loading: 0,
    // 当前显示的数据类型
    teamType: 0,
  },
  onLoad: function (options) {
  },
  onShow: function () {
    // 获取设备信息
    let info = wx.getSystemInfoSync();
    let sysHeight = info.windowHeight - 100;
    this.setData({
        sysHeight: sysHeight,
    });
    // 获取团购订单列表
    this.getUserTeamOrder(this.data.teamType);
  },
  // 查看分享团购
  goGoodsTeam(e){
    let id = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '/pages/goods-team/goods-team?id=' + id,
    });
  },
  // 切换标签页
  switchTab: function (event) {
      let teamType = event.currentTarget.dataset.index;
      this.setData({
          teamType: teamType,
          teamOrder: [],
      });
      this.getUserTeamOrder(teamType);
  },
  // 编辑已有开团信息
  goEditFound(e) {
    let orderId = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '../edit-found/edit-found?orderId=' + orderId,
    })
  },
  // 创建开团
  toFound() {
    wx.navigateTo({
      url: '../found/found',
    })
  },
  // 团购列表
  getUserTeamOrder(status) {
    let that = this;
    let userID = wx.getStorageSync('userID')
    util.requestApi(api.getUserTeamList, {
      userID: userID,
      status:status,
    }).then(function (res) {
      if (res.code == 200) {
        that.setData({
          teamOrder: res.content,
          loading:1
        })
      }
    })
  },
  // 删除正在开的团
  delUserTeamOrder(event) {
    let id = event.currentTarget.dataset.orderid
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除该团购？',
      success(res) {
        if (res.confirm) {
          util.requestApi(api.delUserTeamOrder, {
            teamOrderIDS: [id]
          }).then(function (res) {
            if (res.code == 200) {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000
              })
              that.getUserTeamOrder()
            }
          })
        }
      }
    })
  },
  // 跳转单购买人员列表
  goPayUserList(e) {
    let orderId = e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: '../payUserList/payUserList?orderId=' + orderId,
    })
  },
  // 修改团订单的状态
  setTeamOrderStatus(event) {
    let id = event.currentTarget.dataset.orderid;
    let status = event.currentTarget.dataset.status;
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定修改订单状态吗？',
      success(res) {
        if (res.confirm) {
          util.requestApi(api.setTeamOrderStatus, {
            teamOrderID: id,
            status: status
          }).then(function (res) {
            if (res.code == 200) {
              wx.showToast({
                title: '状态修改成功',
                icon: 'success',
                duration: 2000
              })
              that.getUserTeamOrder()
            }
          })
        }
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.getUserTeamOrder();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  }
})